import request from '@/utils/request'
import type { ApiResponse } from '@/types/customer'

// 定义分页响应类型
interface PaginatedResponse<T> {
  list: T[]
  total: number
  page: number
  limit: number
}

// 会员类型定义
interface Membership {
  id: number
  customer_id: number
  customer_name: string
  level: number
  status: number
  start_time: number
  end_time: number
  amount: number
  create_time: number
  update_time: number
}

// 会员统计数据类型
interface MembershipStats {
  total: number
  active: number
  frozen: number
  expiring: number
}

// 查询参数类型
interface MembershipQueryParams {
  page?: number
  limit?: number
  customer_name?: string
  level?: number
  status?: number
  keywords?: string
}

// 创建/更新会员数据
interface MembershipData {
  customer_id: number
  level: number
  start_time: number
  end_time: number
  amount: number
  remark?: string
}

export const membershipApi = {
  // 获取会员列表
  getMembershipList: (
    params: MembershipQueryParams
  ): Promise<ApiResponse<PaginatedResponse<Membership>>> => {
    return request({
      url: '/api/customer/membership/list',
      method: 'get',
      params,
    })
  },

  // 获取会员统计数据
  getMembershipStats: (): Promise<ApiResponse<MembershipStats>> => {
    return request({
      url: '/api/customer/membership/stats',
      method: 'get',
    })
  },

  // 获取会员详情
  getMembershipDetail: (id: number): Promise<ApiResponse<Membership>> => {
    return request({
      url: `/api/customer/membership/detail/${id}`,
      method: 'get',
    })
  },

  // 开通会员
  createMembership: (data: MembershipData): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/customer/membership/add',
      method: 'post',
      data,
    })
  },

  // 更新会员
  updateMembership: (id: number, data: Partial<MembershipData>): Promise<ApiResponse<any>> => {
    return request({
      url: `/api/customer/membership/edit/${id}`,
      method: 'post',
      data,
    })
  },

  // 删除会员
  deleteMembership: (id: number): Promise<ApiResponse<any>> => {
    return request({
      url: `/api/customer/membership/delete/${id}`,
      method: 'post',
    })
  },

  // 冻结会员
  freezeMembership: (id: number): Promise<ApiResponse<any>> => {
    return request({
      url: `/api/customer/membership/freeze/${id}`,
      method: 'post',
    })
  },

  // 解冻会员
  unfreezeMembership: (id: number): Promise<ApiResponse<any>> => {
    return request({
      url: `/api/customer/membership/unfreeze/${id}`,
      method: 'post',
    })
  },

  // 延期会员
  extendMembership: (
    id: number,
    data: { days: number; amount?: number }
  ): Promise<ApiResponse<any>> => {
    return request({
      url: `/api/customer/membership/extend/${id}`,
      method: 'post',
      data,
    })
  },

  // 获取会员等级选项
  getMembershipLevelOptions: (): Promise<ApiResponse<Array<{ value: number; label: string }>>> => {
    return request({
      url: '/api/customer/membership/levelOptions',
      method: 'get',
    })
  },
}
